package cn.com.gridinfo.sooc.utils.widget.textview;
import android.app.Activity;
import android.text.Editable;
import android.text.Html;
import android.text.Spanned;
import android.text.style.ClickableSpan;
import android.text.style.ImageSpan;
import android.view.View;
import org.xml.sax.XMLReader;
/**
 * Created by Michelle on 2015/7/7.
 */
public class HtmlTagHandler implements Html.TagHandler {
    private Activity activity;

    public HtmlTagHandler(Activity activity) {
        this.activity = activity;
    }

    @Override
    public void handleTag(boolean opening, String tag, Editable output, XMLReader xmlReader) {
        if (tag.toLowerCase().equals("img")) {
            int len = output.length();
            ImageSpan[] images = output.getSpans(len-1, len, ImageSpan.class);
            String imgURL = images[0].getSource();
            output.setSpan(new ImageClick(activity, imgURL), len-1, len, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
    }
    private class ImageClick extends ClickableSpan implements View.OnClickListener{
        private String url;
        private Activity activity;

        public ImageClick(Activity activity, String url) {
            this.activity = activity;
            this.url = url;
        }
        @Override
        public void onClick(View widget) {
//            IntentUtil.start_activity_no_anim(activity, ImagePreviewActivityMy.class, new BasicNameValuePair("imageUrl", url));
        }
    }
}
